import { createRouter, createWebHistory } from 'vue-router'
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
  },
  {
      path: '/',
      name: 'Home',
      component: Home,
      children: [
          {
              path: '/dashboard',
              name: 'dashboard',
              meta: {
                  title: '系统首页',
                  permiss: '1',
              },
              component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
          },
          {
              path: '/salesorder',
              name: 'salesorder',
              meta: {
                  title: '商品销售',
                  permiss: '2',
              },
              component: () => import(/* webpackChunkName: "table" */ '../views/salesorder.vue'),
          },
          {
              path: '/checkorder',
              name: 'checkorder',
              meta: {
                  title: '查看订单',
                  permiss: '11',
              },
              component: () => import(/* webpackChunkName: "charts" */ '../views/checkorder.vue'),
          },
          {
              path: '/form',
              name: 'baseform',
              meta: {
                  title: '表单',
                  permiss: '5',
              },
              component: () => import(/* webpackChunkName: "form" */ '../views/form.vue'),
          },
          {
              path: '/tabs',
              name: 'tabs',
              meta: {
                  title: 'tab标签',
                  permiss: '3',
              },
              component: () => import(/* webpackChunkName: "tabs" */ '../views/tabs.vue'),
          },
          {
              path: '/goods',
              name: 'goods',
              meta: {
                  title: '商品管理',
                  permiss: '14',
              },
              component: () => import(/* webpackChunkName: "donate" */ '../views/goods.vue'),
          },
          {
              path: '/permission',
              name: 'permission',
              meta: {
                  title: '权限管理',
                  permiss: '13',
              },
              component: () => import(/* webpackChunkName: "permission" */ '../views/permission.vue'),
          },
          {
              path: '/upload',
              name: 'upload',
              meta: {
                  title: '上传插件',
                  permiss: '6',
              },
              component: () => import(/* webpackChunkName: "upload" */ '../views/upload.vue'),
          },
          {
              path: '/icon',
              name: 'icon',
              meta: {
                  title: '自定义图标',
                  permiss: '10',
              },
              component: () => import(/* webpackChunkName: "icon" */ '../views/icon.vue'),
          },
          {
              path: '/user',
              name: 'user',
              meta: {
                  title: '个人中心',
              },
              component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'),
          },
          {
              path: '/orderrefund',
              name: 'orderrefund',
              meta: {
                  title: '订单退款',
                  permiss: '2',
              },
              component: () => import(/* webpackChunkName: "export" */ '../views/orderrefund.vue'),
          },
          {
              path: '/goodsvisit',
              name: 'goodsvisit',
              meta: {
                  title: '商品访问量',
                  permiss: '2',
              },
              component: () => import(/* webpackChunkName: "import" */ '../views/goodsvisit.vue'),
          },
      ],
  },
  {
      path: '/login',
      name: 'Login',
      meta: {
          title: '登录',
      },
      component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
  },
  {
      path: '/403',
      name: '403',
      meta: {
          title: '没有权限',
      },
      component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
  }
  ]
})

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

export default router
